package com.yanqu.road.server.cross3server.timer;

import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankMap;
import com.yanqu.road.logic.xlogic.rank.season.XSeasonUserRankMap;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankMap;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.cross3server.manager.Cross3XRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.List;

public class Cross3XRankTask extends GeneralTask {

    @Override
    public void doRun() {

        // 上传 个人榜
        XUserRankMap userRankMap = Cross3XRankMgr.getUserRankMap();
        List<XRankProto.XUserRankListMsg.Builder> cacheList = userRankMap.getSyncCacheAndClear(true);
        for (XRankProto.XUserRankListMsg.Builder builder : cacheList) {
            // 分割后发送
            for (XRankProto.XUserRankListMsg.Builder cutBuilder : XRankPb.cutXUserRankList(builder)) {
                YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(ChannelProtocol.N_XRANK_UPLOAD_USER_RANK_FROM_CROSS, cutBuilder);
                MessageHelper.sendChannelServer(pbMessage);
            }
        }

        // 上传 区域榜 个人贡献
        XChannelRankMap channelRankMap = Cross3XRankMgr.getChannelRankMap();
        List<XRankProto.XChannelRankPartListMsg.Builder> cacheList2 = channelRankMap.getSyncPartCacheAndClear();
        for (XRankProto.XChannelRankPartListMsg.Builder builder : cacheList2) {
            // 分割后发送
            for (XRankProto.XChannelRankPartListMsg.Builder cutBuilder : XRankPb.cutXChannelPartList(builder)) {
                YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(ChannelProtocol.N_XRANK_UPLOAD_CHANNEL_RANK_PART_FROM_CROSS, cutBuilder);
                MessageHelper.sendChannelServer(pbMessage);
            }
        }

        // 上传 赛季个人榜
        XSeasonUserRankMap seasonUserRankMap = Cross3XRankMgr.getSeasonUserRankMap();
        List<XRankProto.XSeasonUserRankListMsg.Builder> cacheSeasonList = seasonUserRankMap.getSyncCacheAndClear(true);
        for (XRankProto.XSeasonUserRankListMsg.Builder builder : cacheSeasonList) {
            // 直接全部发送
            YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(ChannelProtocol.N_XRANK_UPLOAD_SEASON_USER_RANK_FROM_CROSS, builder);
            MessageHelper.sendChannelServer(pbMessage);
        }
    }
}
